package library.dao.Impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import library.dao.IlibraryDao;
import library.db.DbConnection;
import library.pojo.Book;
import library.pojo.BookType;
import library.pojo.BorrowHistory;
import library.pojo.User;
import library.pojo.UserType;

public class BookDaoImpl implements IlibraryDao {

	/**
	 * 修改一本书的状态
	 */
	public void updateStateBorrow(String isbn,String state) {
		Connection conn = DbConnection.getConnection();
		String sql = "update book set state=? where isbn=?";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);	
			ps.setString(1, state);
			ps.setString(2, isbn);
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbConnection.close(ps, null);
		}

	}
	
	/**
	 * 增加一本书
	 */
	@Override
	public void save(Book book) {
		Connection conn = DbConnection.getConnection();
		String sql = "insert into book values(?,?,?,?,?,?)";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, book.getIsbn());
			ps.setString(2, book.getBookName());
			ps.setString(3, book.getAuthor());
			ps.setString(4, book.getPublicationYear());
			ps.setString(5, book.getBookTypeIdx());
			ps.setString(6, book.getState());
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbConnection.close(ps, null);
		}
	}

	/**
	 * 修改一本书的信息
	 */
	@Override
	public void update(Book book) {
		Connection conn = DbConnection.getConnection();
		String sql = "update book set book_name=?,author=?,publication_year=?,book_type_idx=?,state=? where isbn=?";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);	
			ps.setString(1, book.getBookName());
			ps.setString(2, book.getAuthor());
			ps.setString(3, book.getPublicationYear());
			ps.setString(4, book.getBookTypeIdx());
			ps.setString(5, book.getState());
			ps.setString(6, book.getIsbn());
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbConnection.close(ps, null);
		}

	}

	/**
	 * 查询书的信息
	 */
	@Override
	public List<Book> get(String bookname) {
		Connection conn = null;
		ResultSet rs = null;
		PreparedStatement ps = null;
		conn = DbConnection.getConnection();
		String sql = "select * from book " + "where book_name like '%"+bookname+"%'";
		Book book = null;
		List<Book> bookList = new ArrayList<Book>();
		try {
			ps = conn.prepareStatement(sql);
//			ps.setString(1, isbn);
			rs = ps.executeQuery();
			while (rs.next()) {
				String bookIsbn = rs.getString(1);
				String bookName = rs.getString(2);
				String bookAuthor = rs.getString(3);
				String publicationYear = rs.getString(4);
				String bookTypeIdx = rs.getString(5);
				String bookState = rs.getString(6);
				book = new Book(bookIsbn, bookName, bookAuthor, publicationYear, bookTypeIdx, bookState);
				bookList.add(book);
			}
		} catch (SQLException e) {
			System.out.println("Jdbc程序发生错误：" + e);
		} finally {
			DbConnection.close(ps, rs);
		}
		return bookList;
	}
	
	/**
	 * 逐一获取一个图书对象中的全部信息
	 */
	public String getMessage(Book book, int i) {
		Connection conn = null;
		ResultSet rs = null;
		PreparedStatement ps = null;
		conn = DbConnection.getConnection();
		String message = null;
		String sql = "select * from book where isbn = ?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, book.getIsbn());
			rs = ps.executeQuery();
			while (rs.next()) {
				
					message = rs.getString(i);

			}
		} catch (SQLException e) {
		} finally {
			DbConnection.close(ps, null);
		}
		return message;
	}
	/**
	 * 删除一本书的信息
	 */
	@Override
	public void delete(String isbn) {
		Connection conn = null;
		PreparedStatement ps = null;
		conn = DbConnection.getConnection();
		String sql = "delete from book where isbn = ?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, isbn);
			ps.executeUpdate();
		} catch (SQLException e) {
			System.out.println("Jdbc程序发生错误：" + e);
		} finally {
			DbConnection.close(ps, null);
		}
	}

	/**
	 * 查询所有书的信息
	 */
	@Override
	public List<Book> findAll() {
		Connection conn = null;
		ResultSet rs = null;
		PreparedStatement ps = null;
		conn = DbConnection.getConnection();
		String sql = "select * from book";
		Book book = null;
		List<Book> bookList = new ArrayList<Book>();
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				String bookIsbn = rs.getString(1);
				String bookName = rs.getString(2);
				String bookAuthor = rs.getString(3);
				String publicationYear = rs.getString(4);
				String bookTypeIdx = rs.getString(5);
				String bookState = rs.getString(6);
				book = new Book(bookIsbn, bookName, bookAuthor, publicationYear, bookTypeIdx, bookState);
				bookList.add(book);
			}
		} catch (SQLException e) {
			System.out.println("Jdbc程序发生错误：" + e);
		} finally {
			DbConnection.close(ps, rs);
		}
		return bookList;
	}
	
	/**
	 * 查询书的状态
	 */
	public String findBookState(String isbn) {
		Connection conn = null;
		ResultSet rs = null;
		PreparedStatement ps = null;
		String bookState = null;
		conn = DbConnection.getConnection();
		String sql = "select * from book where isbn = ?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, isbn);
			rs = ps.executeQuery();
			while (rs.next()) {
				bookState = rs.getString(6);
			}
		} catch (SQLException e) {
			System.out.println("Jdbc程序发生错误：" + e);
		} finally {
			DbConnection.close(ps, rs);
		}
		return bookState;
	}
	@Override
	public void addBookType(BookType bookType) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void updateBookType(BookType bookType) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public BookType getBookType(String idx) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<BookType> findAllBookType() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void deleteBookType(String idx) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void addUser(User user) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void deleteUser(String username) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void updateUser(User user) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public User getUser(String username) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<User> findAllUser() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void addUserType(UserType userType) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void deleteUserType(String id) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void updateUserType(UserType userType) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public UserType getUserType(String id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<UserType> findAllUserType() {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public void deleteBorrowHistory(String isbn , String borrowUser) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void addBorrowHistory(BorrowHistory borrowHistory) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void updateBorrowHistory(BorrowHistory borrowHistory) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public BorrowHistory getBorrowHistory(String userId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<BorrowHistory> findAllBorrowHistory() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getTypeName(String idx) {
		// TODO Auto-generated method stub
		return null;
	}

}
